package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.ui.UIUtils;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author dimitri
 */
public class TaskView extends JInternalFrame {
    private static final long serialVersionUID = 1484250493080010479L;
    private JPanel centerPanel;
    private TaskSearchPanel searchPanel;
    private TaskQuickView taskQuickView;

    private JButton btnPrint;
    private JButton btnRefresh;
    private JButton btnNew;
    private JButton btnDelete;
    private JToggleButton btnSearch;
    private JButton btnEdit;

    public TaskView() {
        initComponents();
        enableComponents();
    }

    private void initComponents() {
        initGeneralProperties();
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(centerComponent(), BorderLayout.CENTER);
        getContentPane().add(topComponent(), BorderLayout.NORTH);
    }

    private void initGeneralProperties() {
        setClosable(true);
        setMaximizable(true);
        setIconifiable(true);
        setResizable(true);
        setSize(700, 500);
        setTitle("დავალებათა მართვა");
        setFrameIcon(UIUtils.getIcon16x16("email.png"));
    }

    private Component centerComponent() {
        centerPanel = new JPanel(new CardLayout());
        searchPanel = new TaskSearchPanel();
        taskQuickView = new TaskQuickView();
        taskQuickView.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent evnt) {
                enableComponents();
            }
        });
        centerPanel.add(taskQuickView, "quick");
        centerPanel.add(searchPanel, "search");
        return centerPanel;
    }

    private Component topComponent() {
        JToolBar toolBar = new JToolBar();
        toolBar.setFloatable(false);
        btnPrint = new JButton(UIUtils.getIcon16x16("printer.png"));
        btnRefresh = new JButton(UIUtils.getIcon16x16("arrow_refresh.png"));
        btnNew = new JButton(UIUtils.getIcon16x16("add.png"));
        btnDelete = new JButton(UIUtils.getIcon16x16("delete.png"));
        btnSearch = new JToggleButton(UIUtils.getIcon16x16("find.png"));
        btnEdit = new JButton(UIUtils.getIcon16x16("pencil.png"));

        toolBar.add(btnPrint);
        toolBar.addSeparator();
        toolBar.add(btnRefresh);
        toolBar.addSeparator();
        toolBar.add(btnNew);
        toolBar.add(btnEdit);
        toolBar.add(btnDelete);
        toolBar.addSeparator();
        toolBar.add(btnSearch);

        btnEdit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openTask();
            }
        });

        btnPrint.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                taskQuickView.getTable().printSelection();
            }
        });

        btnRefresh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evnt) {
                taskQuickView.getTable().refresh();
            }
        });

        btnNew.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evnt) {
                taskQuickView.getTable().openNew();
            }
        });

        btnDelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evnt) {
                taskQuickView.getTable().deleteTask();
            }
        });

        btnSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                enableComponents();
            }
        });

        return toolBar;
    }

    private void openQuickView() {
        CardLayout layout = (CardLayout) centerPanel.getLayout();
        layout.show(centerPanel, "quick");
    }

    private void openSearch() {
        CardLayout layout = (CardLayout) centerPanel.getLayout();
        layout.show(centerPanel, "search");
    }

    private void enableComponents() {
        boolean search = btnSearch.isSelected();
        boolean selected = !search && taskQuickView.getTable().getSelection() != null;
        btnRefresh.setEnabled(!search);
        btnNew.setEnabled(!search);
        btnDelete.setEnabled(selected);
        btnPrint.setEnabled(selected);
        btnEdit.setEnabled(selected);
        if (search) {
            openSearch();
        } else {
            openQuickView();
        }
    }

    public boolean refresh() {
        return taskQuickView.getTable().refresh();
    }

    public void openTask() {
        boolean search = btnSearch.isSelected();
        if (!search) {
            taskQuickView.openTask();
        } else {
            searchPanel.openTask();
        }
    }

}
